_sambamba()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    local subcommand=${COMP_WORDS[1]}

    if [[ $COMP_CWORD == 1 ]]; then
        COMPREPLY=( $(compgen -W "view index merge sort \
                                  flagstat slice markdup" -- $cur) )
        compopt +o nospace
    elif [[ $cur == -* ]]; then
        case "$subcommand" in
            view)
              COMPREPLY=( $(compgen -W "-F -f -h -H -I -c -S -p \
                                        -l -o -t -s --filter= --header \
                                        --format= --with-header --count \
                                        --reference-info --show-progress \
                                        --sam-input --output-filename= \
                                        --nthreads= --compression-level= \
                                        --subsample= --subsampling-seed=" -- $cur) )
              ;;
            index)
              COMPREPLY=( $(compgen -W "-p -t --nthreads= --show-progress" -- $cur) )
              ;;
            merge)
              COMPREPLY=( $(compgen -W "-p -t --nthreads= -l -H --header \
                                        --compression-level=" -- $cur) )
              ;;
            sort)
              COMPREPLY=( $(compgen -W "-m --memory-limit= --tmpdir= -o --out \
                                        -n --compression-level= -u -p -t \
                                        --uncompressed-chunks --show-progress \
                                        --nthreads=" -- $cur) )
              ;;
            flagstat)
              COMPREPLY=( $(compgen -W "-t -p --nthreads= --show-progress" -- $cur) )
              ;;
            slice)
              COMPREPLY=( $(compgen -W "-o --output-filename=" -- $cur) )
              ;;
            markdup)
              COMPREPLY=( $(compgen -W "-r -t -l -p --tmpdir= --remove-duplicates \
                                        --nthreads= --compression-level= \
                                        --show-progress" -- $cur) )
              ;;
        esac
    
        if [[ ${#COMPREPLY[@]} == 1 && ${COMPREPLY[0]} != "--"*"=" ]] ; then
            compopt +o nospace
        fi

    elif [[ ${COMP_WORDS[COMP_CWORD - 2]} == "--format" || ${COMP_WORDS[COMP_CWORD - 1]} == "-f" ]]; then
        COMPREPLY=( $(compgen -W "sam bam json msgpack" -- $cur) )
        compopt +o nospace
    elif [[ $subcommand == "view" || $subcommand == "slice" ]]; then
        for word in ${COMP_WORDS[@]}; do
            if [[ $word == *.bam && $word != "" ]]; then
                eval abspath=$word
                COMPREPLY=( $(sambamba view -H -t4 $abspath | grep @SQ | \
                              cut -f 2 | cut -f2 -d: 2>/dev/null | grep "^$cur") )
                break
            fi
        done

        if [[ ${#COMPREPLY[@]} == 0 ]]; then
            compopt +o nospace
        fi

        compopt -o default
        compopt -o filenames
    else
        compopt +o nospace
        compopt -o default
    fi
}

complete -o nospace -F _sambamba sambamba
# vim: set syntax=sh :
